package jp.sourceforge.akjrcp.text_douga.effect;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.EffectData;
import jp.sourceforge.akjrcp.text_douga.ImageObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class VideoEffect implements EffectData{
	public static Log log=LogFactory.getLog(VideoEffect.class);
public static final int NULL_TIME=-9999;
private int keyFrame=NULL_TIME;
private double beforeTime=0.2;
private double afterTime=0.2;
private int mode;//default is onetime;
private File exportDir;

protected double fullEffectTime=0.0;//key frame will paint
public double getFullEffectTime() {
	return fullEffectTime;
}

public void setFullEffectTime(double fullPaintTime) {
	this.fullEffectTime = fullPaintTime;
}

public String toString(){
	String out="keyFrame="+keyFrame+" beforeTime="+beforeTime+" afterTime="+afterTime+" mode="+mode+" fullEffect="+fullEffectTime+" exportDir="+exportDir;
	return out;
}


//TODO transparent
public double getAfterTime() {
	return afterTime;
}
public void setAfterTime(double afterTime) {
	this.afterTime = afterTime;
}
public double getBeforeTime() {
	return beforeTime;
}
public void setBeforeTime(double beforeTime) {
	this.beforeTime = beforeTime;
}
public int getKeyFrame() {
	return keyFrame;
}
public void setKeyFrame(int keyFrame) {
	this.keyFrame = keyFrame;
}

public boolean writeFrame(DougaPreference preference,ImageObject baseImage,int frame) throws IOException{
	if(mode==DougaSystem.FOREEVER){//maybe this is background image
		effectAll(preference,baseImage.getImage(),frame);
		return true;
	}
	
	double keyTime=new BigDecimal(""+keyFrame).divide(new BigDecimal(""+preference.getFps())).doubleValue();
	double frameTime=new BigDecimal(""+frame).divide(new BigDecimal(""+preference.getFps())).doubleValue();//(frame-1) TODO
	
	
	
	if(frame==keyFrame){
	log.info("effect-keyframe: frame="+frame);
	effectKeyFrame(preference,baseImage.getImage(),frame,0);	
	return true;
	}
	//log.info(keyTime+","+frameTime);
	
	
	if(frameTime<keyTime && frameTime>keyTime-beforeTime){
		double time=frameTime-keyTime;
		log.info("before-effect:"+frameTime+","+time+" frame="+frame);
		effectBefore(preference,baseImage.getImage(),frame,time);
		return true;
	}else if(frameTime>keyTime && frameTime<keyTime+afterTime){
		double time=frameTime-keyTime;
		log.info("after-effect:"+frameTime+","+time+" frame="+frame);
		effectAfter(preference,baseImage.getImage(),frame,time);
		return true;
	}
	return false;
}



public abstract void effectAll(DougaPreference preference,BufferedImage baseImage,int frame) throws IOException;
public abstract void effectBefore(DougaPreference preference,BufferedImage baseImage,int frame,double time) throws IOException;
public abstract void effectAfter(DougaPreference preference,BufferedImage baseImage,int frame,double time) throws IOException;
public abstract void effectKeyFrame(DougaPreference preference,BufferedImage baseImage,int frame,double time) throws IOException;
public int getMode() {
	return mode;
}
public void setMode(int mode) {
	this.mode = mode;
}
public File getExportDir() {
	return exportDir;
}
public void setExportDir(File exportDir) {
	this.exportDir = exportDir;
}
}
